Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/redis state management - Close #13 #27

Merged
merged 4 commits into from
May 20, 2024

Conversation

seyedali-dev
Copy link
Owner

Fix: Resolved LazyInitializationException in TimeEntry retrieval. (Close #13)

Issue

We encountered a LazyInitializationException when trying to access the timeSegmentList of a TimeEntry object that was retrieved from the cache. This happened because the timeSegmentList was lazily loaded by Hibernate, and we were trying to access it after the Hibernate Session had been closed.

Temporary Solution

We resolved this issue by forcing the initialization of the timeSegmentList before caching the TimeEntry object. We did this by calling size() on the timeSegmentList in the getTimeEntryById method. This ensured that the list was fetched from the database and included in the cached TimeEntry.

timeEntry.getTimeSegmentList().size(); // This will initialize the timeSegmentList

Note

This solution might have performance implications if the timeSegmentList is large, as it will always be fetched from the database even if it’s not needed. Therefore, this solution is not recommended for large data sets. We are currently exploring more efficient solutions.

This commit implements Redis caching for the `TimeEntry` object to address the following requirements:

- Persistence of Time Entry state across restarts: TimeEntry objects are now cached in Redis, allowing the application to recover the state after a restart or crash.

- Scalability and consistency across multiple servers: By storing TimeEntry data in Redis, all instances of the application can access the same data, ensuring consistency when scaled across multiple servers.

- Improved performance for time-sensitive operations: Caching TimeEntry objects in Redis reduces the overhead of fetching data from the database, leading to better performance for time-sensitive operations like time tracking.

The following changes were made:

- Added `TimeEntryCacheManager` class to handle Redis caching operations for TimeEntry objects.
- Implemented caching of TimeEntry objects in relevant methods of `TimeEntryServiceImpl` and `TimeEntryTrackingServiceImpl` using `@Cacheable` and `@CachePut` annotations.
- Implemented cache eviction for the `TimeEntryService#deleteTimeEntry(TimeEntryId)` method using the `@CacheEvict` annotation.

With these changes, the application now meets the specified requirements for state management, scalability, and performance using Redis as a caching solution.
 #13)

# Issue
We encountered a `LazyInitializationException` when trying to access the `timeSegmentList` of a `TimeEntry` object that was retrieved from the cache. This happened because the `timeSegmentList` was lazily loaded by Hibernate, and we were trying to access it after the Hibernate Session had been closed.

# Temporary Solution
We resolved this issue by forcing the initialization of the `timeSegmentList` before caching the `TimeEntry` object. We did this by calling `size()` on the `timeSegmentList` in the `getTimeEntryById` method. This ensured that the list was fetched from the database and included in the cached `TimeEntry`.

```java
timeEntry.getTimeSegmentList().size(); // This will initialize the timeSegmentList
```
> ## Note
>  This solution might have performance implications if the timeSegmentList is large, as it will always be fetched from the database even if it’s not needed. Therefore, this solution is not recommended for large data sets. We are currently exploring more efficient solutions.
@seyedali-dev seyedali-dev added bug Something isn't working enhancement Enhance a feature new feature New feature labels May 20, 2024
@seyedali-dev seyedali-dev self-assigned this May 20, 2024
@seyedali-dev seyedali-dev merged commit 6231710 into main May 20, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement Enhance a feature new feature New feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Redis for State Management of Time Tracking Time Entry
1 participant